<?php
// +----------------------------------------------------------------------
// | HkCms 网站前台
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2021 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\admin\model\cms\Model;
use app\index\model\cms\Category;
use think\facade\Db;

class Index extends BaseController
{
    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 内容列表
     * @return string|void
     */
    public function lists()
    {
        // 栏目ID
        $catId = $this->request->param('cid','');
        // 列表栏目
        $list = $this->request->param('list','');

        $cateInfo = (new Category)->getCateInfo(empty($catId)?$list:$catId);
        if (empty($cateInfo)) {
            $this->error(lang('Column doesn\'t exist.'));
        }
        $model = Model::where(['id'=>$cateInfo['model_id']])->find();
        if (empty($model)) {
            $this->error(lang('Model doesn\'t exist!'));
        }

        if ('category'==$cateInfo['type']) {
            // 栏目首页模板
            $template = 'category/'.(!empty($cateInfo['category_tpl']) ? $cateInfo['category_tpl']:'category');
        } else if ('list'==$cateInfo['type']) {
            $template = 'list/'.(!empty($cateInfo['list_tpl']) ? $cateInfo['list_tpl']:'list');
        } else if ('link'==$cateInfo['type'] && $cateInfo['model_id']) {
            $c = '\app\admin\model\cms\\'.$model->controller;
            $info = (new $c)->setTable($model->tablename)->where(['category_id'=>$cateInfo['id']])->find();
            $info = empty($info) ? [] : $info->toArray();
            $this->view->assign('Info', $info);
            if (!empty($info['show_tpl'])) {
                $cateInfo['show_tpl'] = $info['show_tpl'];
            }
            $template = 'page/'.(!empty($cateInfo['show_tpl']) ? $cateInfo['show_tpl']:'page');
        } else {
            $this->error(lang('The page doesn\'t exist.'));
        }
        $template = explode(".", $template, 2);
        $this->view->assign('Cate', $cateInfo);
        $this->view->assign('seo_title', empty($cateInfo['seo_title'])?$cateInfo['title']:$cateInfo['seo_title']);
        $this->view->assign('seo_keywords', $cateInfo['seo_keywords']);
        $this->view->assign('seo_desc', $cateInfo['seo_desc']);
        return $this->view->fetch($template[0]);
    }

    /**
     * 详情页
     * @return string|void
     */
    public function show()
    {
        // 栏目ID
        $catId = $this->request->param('cid','', 'intval');
        // 栏目英文
        $list = $this->request->param('list','');
        // 文章ID
        $id = $this->request->param('id', 0, 'intval');

        $key = $this->request->param('key','');

        $cateInfo = (new Category)->getCateInfo(is_numeric($catId)?$catId:$list);
        if (empty($cateInfo)) {
            $this->error(lang('The page doesn\'t exist.'));
        }
        $model = Model::where(['id'=>$cateInfo['model_id'],'status'=>'normal'])->find();
        if (empty($model)) {
            $this->error(lang('Model doesn\'t exist!'));
        }
        $type = Db::name('model_controller')->where(['status'=>'normal','name'=>$model->controller])->value('type');
        $c = '\app\admin\model\cms\\'.$model->controller;

        if (!empty($key) && md5(app('session')->getId())==$key) { // 授权临时访问禁用的文章
            $info = (new $c)->setTable($model->tablename)->where(['category_id'=>$cateInfo['id'],'id'=>$id])->find();
        } else {
            $info = (new $c)->setTable($model->tablename)->where(['category_id'=>$cateInfo['id'],'id'=>$id,'status'=>'normal'])->find();
        }

        if (empty($info)) {
            $this->error(lang('The document doesn\'t exist.'));
        }
        $info = $info->toArray();
        if ($type=='more') {
            $obj = Db::name($model->tablename.'_data')->where(['id'=>$info['id']])->find();
            if (isset($obj['content'])) {
                $obj['content'] = htmlspecialchars_decode($obj['content']);
            }
            $info['more'] = empty($obj) ? [] : $obj;
        }

        Db::name($model->tablename)->where(['id'=>$id])->inc('views')->update();
        $this->view->assign('Cate', $cateInfo);
        $this->view->assign('Info', $info);

        // seo 模型固定的默认字段 keywords description
        $this->view->assign('seo_title', $info['title'].' - '.$cateInfo['title']);
        $this->view->assign('seo_keywords', isset($info['keywords'])?$info['keywords']:$cateInfo['seo_keywords']);
        $this->view->assign('seo_desc', isset($info['description'])?$info['description']:$cateInfo['seo_desc']);
        $template = explode(".", $info['show_tpl'], 2);
        return $this->view->fetch('show/'.$template[0]);
    }
}
